package vn.edu.ptithcm.pim.util;

public class DocumentUtil {
	public static String PDF_DOCUMENT_EXTENSION = ".pdf";
	private static String[] imageExtensions = { ".jpeg", ".jpg", ".png" };
	private static String[] officeExtensions = { ".doc", ".docx", ".ppt",
			".pptx", ".odt", ".odp", ".rtf" };

	public static boolean isPdfDocument(String documentPath) {
		String extension = getFileExtension(documentPath);
		return PDF_DOCUMENT_EXTENSION.indexOf(extension) != -1;
	}

	public static boolean isImageDocument(String documentPath) {
		String extension = getFileExtension(documentPath);
		return isExtensionInGroup(extension, imageExtensions);
	}

	public static boolean isOfficeDocument(String documentPath) {
		System.out.println("get extension for " + documentPath);
		String extension = getFileExtension(documentPath);
		System.out.println("the result is : " + extension);
		return isExtensionInGroup(extension, officeExtensions);
	}

	private static boolean isExtensionInGroup(String extension,
			String[] extensions) {
		boolean result = false;
		for (String ext : extensions) {
			if (ext.indexOf(extension) != -1) {
				return true;
			}
		}
		return result;
	}

	public static String getFileExtension(String documentPath) {
		int extension = documentPath.lastIndexOf('.');
		if (extension != -1) {
			return documentPath.substring(extension);
		}
		return "";
	}

	public static String getFileName(String documentPath) {
		if (isNotNullAndEmpty(documentPath)) {
			int extension = documentPath.lastIndexOf('.');
			if (extension != -1) {
				return documentPath.substring(0, extension);
			}
		}
		return "";
	}

	public static boolean isNotNullAndEmpty(String s) {
		return s != null && s.trim().length() > 0;
	}
}
